package com.lwj.liwanjia.view.activity.login;


import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.lwj.liwanjia.MainActivity;
import com.lwj.liwanjia.R;
import com.lwj.liwanjia.base.BaseActivity;
import com.lwj.liwanjia.bean.LoginBean;
import com.lwj.liwanjia.impl.LoginView;
import com.lwj.liwanjia.presenter.LoginPresenter;
import com.lwj.liwanjia.utils.SpfUtils;

import java.util.ArrayList;
import java.util.List;

import cn.jpush.android.api.JPushInterface;

/**
 * 登录界面
 */
public class LoginActivity extends BaseActivity implements View.OnClickListener, LoginView {

    private ImageView iv_left;//头部局左边按钮
    private TextView tv_title;//头部局中间标题
    private Button bt_login;
    private TextView tv_register;
    private TextView tv_forget;
    private LoginPresenter loginPresenter;
    private SpfUtils spfUtis;
    private EditText tvAccount;
    private EditText tvPwd;
    private String account;
    private String type;

    @Override
    protected void initView() {
        iv_left = (ImageView) findViewById(R.id.iv_left);
        tv_title = (TextView) findViewById(R.id.tv_title);
        bt_login = (Button) findViewById(R.id.bt_login);
        tv_register = (TextView) findViewById(R.id.tv_register);
        tv_forget = (TextView) findViewById(R.id.tv_forget);
        bt_login.setOnClickListener(this);
        tv_register.setOnClickListener(this);
        tv_forget.setOnClickListener(this);

        //初始化布局
        iv_left.setVisibility(View.GONE);
        tv_title.setText("登陆账号");

        tvAccount = (EditText) findViewById(R.id.tv_account);
        tvPwd = (EditText) findViewById(R.id.tv_pwd);
        spfUtis = SpfUtils.getSpfUtils(this);
        loginPresenter = new LoginPresenter(this);
    }

    @Override
    protected void initData() {
        type = getIntent().getStringExtra("type");
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_login;
    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) {
            //登陆到主页
            case R.id.bt_login:
                account = tvAccount.getText().toString();
                String pwd = tvPwd.getText().toString();
                if (TextUtils.isEmpty(account) || TextUtils.isEmpty(pwd)) {
                    Toast.makeText(this, "账号密码不能为空！", Toast.LENGTH_SHORT).show();
                    return;
                }
                loginPresenter.getToken(account, pwd);
                break;
            //注册
            case R.id.tv_register:
                startActivity(new Intent(LoginActivity.this, RegisterActivity.class));
                break;
            //忘记密码
            case R.id.tv_forget:
                startActivity(new Intent(LoginActivity.this, ForgetActivity.class));
                break;
        }
    }

    @Override
    public void showError(Exception e) {
        Toast.makeText(this, "访问网络失败", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void getToken(LoginBean loginBean) {
        if (loginBean.getState() == 0) {
            Toast.makeText(this, "登录失败", Toast.LENGTH_SHORT).show();
        } else if (loginBean.getState() == 1) {
            JPushInterface.setAlias(this, 0, account);
            spfUtis.setStoken(loginBean.getData());
            if (type != null && (type.equals("购物车") || type.equals("个人中心") || type.equals("退出登录"))) {
                startActivity(new Intent(this, MainActivity.class).putExtra("type", type));
                finish();
            } else {
                finish();
            }
        } else if (loginBean.getState() == 2) {
            Toast.makeText(this, "账号错误", Toast.LENGTH_SHORT).show();
        } else if (loginBean.getState() == 3) {
            Toast.makeText(this, "密码错误", Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    protected void onDestroy() {
        if (loginPresenter != null) {
            loginPresenter = null;
        }
        super.onDestroy();
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    public void onBackPressed() {
        setIntent();
    }

    private void setIntent() {
        if (type != null && (type.equals("购物车") || type.equals("个人中心") || type.equals("一卡通"))) {
            startActivity(new Intent(this, MainActivity.class));
            finish();
        } else {
            finish();
        }
    }
}
